// 字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 
// 给定两个字符串，编写一个函数判定它们是否只需要一次(或者零次)编辑。

/**
 * @param {string} first
 * @param {string} second
 * @return {boolean}
 */
 var oneEditAway = function(first, second) {
    let diff = 0;
    console.log("first:",first,"second:",second);
    if(first === second || first.length===1&&second.length===1){
        return true;
    }
    // first = Array.from(first).sort();
    // second = Array.from(second).sort();
    // console.log("first:",first,"second:",second);



    if(first.length !== second.length){
        let long = first.length > second.length ? first : second;
        let short =  first.length < second.length ? first : second;
        console.log("long:",long,"short:",short);
    
        if(long.length - 1 === short.length){
            for(let i = 0;i<short.length;i++){
                console.log(long.indexOf(short[i]))
                if(long.indexOf(short[i]) === -1){
                    // return false;
                    diff++;
                }
            }
        }else{
            return false
        }
    }else{
        for(let i = 0;i<second.length ;i++){
            // console.log((second[i]),(second[i+1]))
            // console.log(first.indexOf(second[i]),first.indexOf(second[i+1]))
            // if( first.indexOf(second[i]) === -1 && first.indexOf(second[i+1]) === -1){
                if( first.indexOf(second[i]) === -1){
                // return false;
                diff++;
            }else{
                continue;
            }
        }
    }
    console.log(diff);
    return diff === 1 ? true : false;
    // return true
};

// let first = "pale",second = "ple"
// let first = "horse",second = "ros"
// let first = "a",second = "b"
// let first = "teacher",second = "beacher"
let first = "ab",second = "bc"
// let first = "sea",second = "ate"// false
// let first = "prosperity",second = "properties"//false





console.log(oneEditAway(first,second));